#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char p[1001][101],s[1001][1001];
int bz[10001];
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%s%s",p[i],s[i]);
		int t=strlen(s[i])-1,x=0,y=0,z=0,bj;
		for(int j=0;j<=t;j++)
		{
			if(s[i][j]>='0'&&s[i][j]<='9')
			{
				if(j==0||s[i][j-1]=='.'||s[i][j]-1==':')
				{
					if(s[i][j]=='0'&&(s[i][j+1]>='0'&&s[i][j+1]<='9'))
					{
						bz[i]=-1;
						break;
					}
				}
				z=z*10+s[i][j]-'0';
				if(bj==0)
				{
					if(z>255)
					{
						bz[i]=-1;
						break;
					}
				}
				if(bj==1)
				{
					if(z>65535)
					{
						bz[i]=-1;
						break;
					}
				}
			}
			if(s[i][j]=='.')
			{
				if(y!=0)
				{
					bz[i]=-1;
					break;
				}
				x++;
				z=0;
				bj=0;
				if(x>3)
				{
					bz[i]=-1;
					break;
				}
			}
			if(s[i][j]==':')
			{
				if(x!=3)
				{
					bz[i]=-1;
					break;
				}
				y++;
				z=0;
				bj=1;
				if(y>1)
				{
					bz[i]=-1;
					break;
				}
			}
		}
		if(x!=3||y!=1)
		{
			bz[i]=-1;
		}
		if(bz[i]!=-1)
		{
			if(p[i][0]=='S')
			{
				for(int j=1;j<=i-1;j++)
				{
					if(p[j][0]=='S'&&bz[j]!=-1&&strcmp(s[i],s[j])==0)
					{
						bz[i]=-2;
						break;
					}
				}
			}
			if(p[i][0]=='C')
			{
				for(int j=1;j<=i-1;j++)
				{
					if(p[j][0]=='S'&&bz[j]!=-1&&bz[j]!=-2&&strcmp(s[i],s[j])==0)
					{
						bz[i]=j;
						bz[j]=-3;
						break;
					}
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(bz[i]==-1)
		{
			printf("ERR\n");
		}
		if(bz[i]==-2||(bz[i]==0&&p[i][0]=='C'))
		{
			printf("FAIL\n");
		}
		if(bz[i]==-3||(bz[i]==0&&p[i][0]=='S'))
		{
			printf("OK\n");
		}
		if(bz[i]>0)
		{
			printf("%d\n",bz[i]);
		}
	}
}
